﻿#region Copyright
// Copyright (c) 2011-2012 RaisingForce Team (Alikin Sergey)
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
#endregion

using System;
using System.Diagnostics.Contracts;
using System.Globalization;

namespace RaisingForce.Raise.Presentation.Localization
{
    sealed public class LocalizationLanguage
    {
        #region Constructors

        private LocalizationLanguage(
            string cultureName,
            string displayName,
            string iconPath,
            bool isInvariant)
        {
            Contract.Requires(cultureName != null);
            Contract.Requires(MessageHelper.MessageIsValid(displayName) || displayName == null);
            this.isInvariant = isInvariant;
            this.cultureName = cultureName;
            this.displayName = displayName;
            this.iconPath = iconPath;
        }

        #endregion

        #region Constants

        public const string InvariantCultureName = "Invariant";

        #endregion

        #region Properties

        private readonly bool isInvariant;

        public bool IsInvariant
        {
            get { return this.isInvariant; }
        }

        private readonly string cultureName;

        public string CultureName
        {
            get
            {
                Contract.Ensures(Contract.Result<string>() != null);
                return this.cultureName;
            }
        }

        private string displayName;

        public string DisplayName
        {
            get
            {
                Contract.Ensures(MessageHelper.MessageIsValid(displayName));
                if (this.displayName == null)
                {
                    this.displayName = this.CultureInfo.EnglishName;
                }
                return this.displayName;
            }
        }

        private readonly string iconPath;

        public string IconPath
        {
            get
            {
                // значение может быть null или неверным URI / file path
                return this.iconPath;
            }
        }

        private WeakReference cultureInfo;

        private readonly object syncObject = new object();

        public CultureInfo CultureInfo
        {
            get
            {
                Contract.Ensures(Contract.Result<CultureInfo>() != null);
                lock (this.syncObject)
                {
                    if (this.cultureInfo == null
                        || !this.cultureInfo.IsAlive)
                    {
                        CultureInfo info = this.isInvariant ?
                            CultureInfo.InvariantCulture :
                            CultureInfo.GetCultureInfo(this.CultureName);
                        Contract.Assume(info != null);
                        this.cultureInfo = new WeakReference(info);
                        return info;
                    }
                    else
                    {
                        return (CultureInfo)this.cultureInfo.Target;
                    }
                }
            }
        }

        #endregion

        #region Static Object Creators

        public static LocalizationLanguage Create(
            string cultureName,
            string displayName = null,
            string iconPath = null)
        {
            if (cultureName == null)
            {
                throw new ArgumentNullException("cultureName");
            }
            if (!MessageHelper.MessageIsValid(displayName)
                && displayName != null)
            {
                throw new ArgumentException("displayName");
            }
            Contract.Ensures(Contract.Result<LocalizationLanguage>() != null);
            Contract.EndContractBlock();
            return new LocalizationLanguage(
                cultureName,
                displayName,
                iconPath,
                isInvariant: false);
        }

        public static LocalizationLanguage CreateInvariant(
            string displayName = null,
            string iconPath = null)
        {
            Contract.Ensures(Contract.Result<LocalizationLanguage>() != null);
            return new LocalizationLanguage(
                InvariantCultureName,
                displayName,
                iconPath,
                isInvariant: true);
        }

        #endregion
    }
}
